package com.leicx.guli.coupon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.leicx.guli.common.to.MemberPrice;
import com.leicx.guli.common.to.SkuReductionTo;
import com.leicx.guli.coupon.entity.MemberPriceEntity;
import com.leicx.guli.coupon.entity.SkuLadderEntity;
import com.leicx.guli.coupon.service.MemberPriceService;
import com.leicx.guli.coupon.service.SkuLadderService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leicx.guli.common.utils.PageUtils;
import com.leicx.guli.common.utils.Query;

import com.leicx.guli.coupon.dao.SkuFullReductionDao;
import com.leicx.guli.coupon.entity.SkuFullReductionEntity;
import com.leicx.guli.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    SkuLadderService skuLadderService;
    @Resource
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 保存满打折信息
        if (skuReductionTo.getFullCount() > 0) {
            SkuLadderEntity skuLadderEntity = BeanUtil.copyProperties(skuReductionTo, SkuLadderEntity.class);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        // 保存满减信息
        if (skuReductionTo.getFullPrice().compareTo(BigDecimal.ZERO) > 0) {
            SkuFullReductionEntity skuFullReductionEntity = BeanUtil.copyProperties(skuReductionTo, SkuFullReductionEntity.class);
            skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
            save(skuFullReductionEntity);
        }

        // 保存会员价格信息
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream()
                .map(q -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                    memberPriceEntity.setMemberLevelId(q.getId());
                    memberPriceEntity.setMemberLevelName(q.getName());
                    memberPriceEntity.setMemberPrice(q.getPrice());
                    memberPriceEntity.setAddOther(1);
                    return memberPriceEntity;
                })
                .filter(q -> q.getMemberPrice().compareTo(BigDecimal.ZERO) > 0)
                .collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}